define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/coupon/index' + location.search,
                    add_url: 'user/coupon/add',
                    edit_url: 'user/coupon/edit',
                    del_url: 'user/coupon/del',
                    multi_url: 'user/coupon/multi',
                    import_url: 'user/coupon/import',
                    table: 'user_coupon',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='store_id']", form).addClass("selectpage").data("source", "store/ajaxgetstorewithquanoption").data("primaryKey", "value").data("field", "name");
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: __('User_id'), operate: 'LIKE'},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'LIKE'},
                        // {field: 'coupon_type', title: __('Coupon_type'), searchList: {"1":__('Coupon_type 1'),"2":__('Coupon_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'reduce_price', title: __('Reduce_price'), operate:'BETWEEN', formatter: Controller.api.show_reduce_price},
                        // {field: 'discount', title: __('Discount'),  formatter: Controller.api.show_discount},
                        // {field: 'min_price', title: __('Min_price'), operate:'BETWEEN'},
                        {field: 'expire_type', title: __('Expire_type'), searchList: {"1":__('Expire_type 1'),"2":__('Expire_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'expire_time', title: __('Expire_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Controller.api.show_expire_time},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Controller.api.show_start_end_day},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Controller.api.show_start_end_day},
                        {field: 'store_id', title: __('Store_id'), operate: 'LIKE', visible: false},
                        {field: 'store.store_name', title: __('Store_id'), operate: false, formatter: Controller.api.showstore},
                        {field: 'room.room_name', title: __('Room_id'), operate: false, formatter: Controller.api.showroom},
                        {field: 'is_used', title: __('Is_used'), searchList: {"1":__('Is_used 1'),"0":__('Is_used 0')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                search: false,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            show_reduce_price: function (e, row) {
                if (1 == row.coupon_type) {
                    return e
                } else {
                    return '-'
                }
            },
            show_discount: function (e, row) {
                if (2 == row.coupon_type) {
                    return e / 10 + '折'
                } else {
                    return '-'
                }
            },
            show_expire_time: function (e, row) {
                if (1 == row.expire_type) {
                    return e
                } else {
                    return '-'
                }
            },
            show_start_end_day: function (e, row) {
                if (2 == row.expire_type) {
                    return e
                } else {
                    return '-'
                }
            },
            showstore: function (e, row) {
                if (0 == row.store_id) {
                    return '全部门店'
                }
                if (null == e) {
                    return '-'
                }
                return e
            },
            showroom: function (e, row) {
                if (0 == row.room_id) {
                    return '全部教室'
                }
                if (null == e) {
                    return '-'
                }
                return e
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
